package Server;

import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.util.ArrayList;

import org.feiteira.network.copy.SeriDataPackage;
import org.feiteira.network.copy.SeriEventHandler;
import org.feiteira.network.copy.SeriServer;

import com.googlecode.cqengine.CQEngine;
import com.googlecode.cqengine.IndexedCollection;

import NET.NetPlayer;

public class ClientHandler implements SeriEventHandler {
   private IndexedCollection<NetPlayer> players;

   public ClientHandler() {
      players = CQEngine.newInstance();
      players.add(new NetPlayer(25, 30, 5, 20, NetPlayer.State.NORMAL, "WIZ"));
      players.add(new NetPlayer(15, 20, 15, 2, NetPlayer.State.DEAD, "TEST"));
      players.add(new NetPlayer(5, 10, 15, 60, NetPlayer.State.NORMAL, "BLUE"));
      players.add(new NetPlayer(45, 45, 6, 60, NetPlayer.State.DYING, "RED"));
   }

   @Override
   public void messageArrived(SeriDataPackage pack) {
      Object fromClient = pack.getObject();
      System.out.println("Client: " + fromClient.toString());
      Serializable objectToSend;
      if (fromClient.equals("Bye.")) {
         objectToSend = "Bye.";
      } else {
         ArrayList<NetPlayer> toSend = new ArrayList<NetPlayer>();
         toSend.addAll(players);
         objectToSend = toSend;
      }
      try {
         SeriServer.reply(pack, objectToSend);
      } catch (IOException e) {
         e.printStackTrace();
      }

   }

   @Override
   public void shutdownCompleted() {
      // TODO Auto-generated method stub

   }
}